登录 白背景

5938. 找出数组排序后的目标下标

https://leetcode-cn.com/problems/find-target-indices-after-sorting-array/

  • 提交时间:2021-11-28 05:11:37
  • 执行用时:4 ms, 在所有 Go 提交中击败了100.00%的用户
  • 内存消耗:2.8 MB, 在所有 Go 提交中击败了100.00%的用户
  • 通过测试用例:216 / 216
func targetIndices(nums []int, target int) (ans []int) {
    minCount := 0
    total := 0
    for _, item := range nums {
        if item < target {
            minCount++
        }
        if item == target {
            total++
        }
    }
    if total == 0 {
        return ans
    }
    ans = []int{}
    for i := 0; i < total; i++ {
        ans = append(ans, minCount+i)
    }
    return ans
}